﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml.Serialization;

namespace FYP.Profilings
{
    public class DataManager
    {
        private Game _game;

        private string _name;

        private List<PerformanceData> _data;
        private int _currentIndex = -1;

        private double _averageFPS;

        private bool _isSaved;

        /// <summary>
        /// Initializes a new instance of the DataManager class.
        /// </summary>
        public DataManager()
        {
        }
        
        public DataManager(Game game)
        {
            _game = game;
            _data = new List<PerformanceData>();
        }

        public void Start(GameTime gameTime)
        {
            if (CurrentData != null)
            {
                CurrentData.Start(gameTime);
            }
        }
        public void End(GameTime gameTime)
        {
            if (CurrentData != null)
            {
                CurrentData.End(gameTime);
            }
        }
        public void Update(GameTime gameTime)
        {
            if (CurrentData != null && !CurrentData.IsEnded && CurrentData.IsStarted)
            {
                _isSaved = false;
                CurrentData.Update(gameTime);
            }
        }

        public void Draw(GameTime gameTime)
        {
            if (CurrentData != null && !CurrentData.IsEnded && CurrentData.IsStarted)
            {
                _isSaved = false;
                CurrentData.Draw(gameTime);
            }
        }

        /// <summary>
        /// Add a new performance data to the list and move the current index to this new one
        /// </summary>
        public void AddNewData()
        {
            PerformanceData performanceData = new PerformanceData(Game);
            _data.Add(performanceData);
            _currentIndex = _data.Count - 1;
        }

        public void Save(string fileName)
        {
            // Calculate the average FPS for the whole test
            _averageFPS = 0;
            for (int i = 0; i < _data.Count; i++)
            {
                _averageFPS += _data[i].AverageFPS;
            }
            _averageFPS /= _data.Count;

            zEngine.FileIO.SaveSerializeToXML(this, fileName);
            _isSaved = true;
        }

        #region Properties
        [XmlAttribute]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public double AverageFPS
        {
            get { return _averageFPS; }
            set { _averageFPS = value; }
        }
        [XmlIgnore]
        public PerformanceData CurrentData
        {
            get
            {
                if (_currentIndex > -1 && _currentIndex < _data.Count)
                {
                    return _data[_currentIndex];
                }
                return null;
            }
        }
        [XmlIgnore]
        public int CurrentIndex
        {
            get { return _currentIndex; }
            set { _currentIndex = value; }
        }

        public List<PerformanceData> Data
        {
            get { return _data; }
            set { _data = value; }
        }
        [XmlIgnore]
        public bool IsSaved
        {
            get { return _isSaved; }
            set { _isSaved = value; }
        }
        [XmlIgnore]
        public Game Game
        {
            get { return _game; }
            set { _game = value; }
        }
        #endregion

    }
}
